<?php
namespace App\Service\Item\Fxotary\Front;

use App\Service\Item\Fxotary\BaseService as Service;
use Illuminate\Support\Facades\DB;

class MenuService extends Service
{
    //菜单

    /**
     * 列表
     * @param int $item_id 项目ID
     */
    public function getList($item_id)
    {
        $res = $this->getChildList($item_id);
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    /**
     * 获取子菜单
     * @param int $item_id 项目ID
     * @param int $pid 父级ID
     */
    public function getChildList($item_id, $pid = 0)
    {
        $arys = [];
        $res = $this->dbConn($this->xmMenu)
            ->where('is_show', 1)
            ->where('type', 1)
            ->where('item_id', $item_id)
            ->where('pid', $pid)
            ->get();
        if (!count($res)) {
            return $arys;
        }
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'link' => $v['link'] ? $v['link'] : '',
                'childList' => $this->getChildList($item_id, $v['id']),
            ];
        }
        return $arys;
    }

    /**
     * 页脚内容
     * @param int $item_id 项目ID
     */
    function foot($item_id)
    {
        $this->toback();
        //项目信息
        $res = (array) $this->dbConn($this->dbTmp)
            ->where('id', $item_id)
            ->first();
        if (empty($res) || !$res['qiyeJson']) {
            return $this->toback(-2, '没有信息');
        }
        $jsons = json_decode($res['qiyeJson'], true);
        //项目菜单
        $menus = $this->dbConn($this->xmMenu)
            //->where('item_id', $item_id)
            ->where('type', 1)
            ->where('pid', 0)
            ->get();
        if (!count($menus)) {
            return $this->toback(-2, '没有信息');
        }
        $arys = [
            'logo' => $jsons['logo'],
            'detail' => $jsons['detail'],
            'street' => $jsons['street'],
            'door' => $jsons['door'],
            'phone' => $jsons['phone'],
            'email' => $jsons['email'],
        ];
        foreach ($menus as $v) {
            $v = (array) $v;
            $link = $v['link'];
            $child_id = 0;
            if (!$link) {
                $childOne = (array) $this->dbConn($this->xmMenu)
                    ->where('item_id', $item_id)
                    ->where('pid', $v['id'])
                    ->first();
                $child_id = empty($childOne) ? 0 : $childOne['id'];
                $link = empty($childOne) ? '' : $childOne['link'];
            }
            if (!$link && $child_id) {
                $childOne = (array) $this->dbConn($this->xmMenu)
                    ->where('item_id', $item_id)
                    ->where('pid', $child_id)
                    ->first();
                $link = empty($childOne) ? '' : $childOne['link'];
            }
            if ($link) {
                $field = ucfirst(strtolower($v['title']));
                $arys['menus'][] = ['title' => $field, 'link' => $link];
            }
        }
        return $this->toback(0, '', $arys);
    }
}